﻿/*global analogue*//*jslint bitwise: true, browser: true, eqeqeq: true, immed: true, newcap: true, nomen: true, onevar: true, plusplus: true, regexp: true, undef: true, white: true, indent: 4*/

(function (window) {
    analogue.extend(Function.prototype, {
        bind: analogue.bind,
        extend: analogue.extend,
        inherit: function (object) {
            this.prototype = object;
            return this;
        },
        inherits: function (Parent, inheritance) {
            this.constructor = Parent;
            this.prototype = new Parent();
            if (inheritance) {
                this.extend(this.prototype, inheritance);
            }
            return this;
        }
    });
    analogue.extend(window, {
        addEvent: function () {
            return analogue.events.add.apply(window, arguments);
        },
        removeEvent: function () {
            return analogue.events.remove.apply(window, arguments);
        },
        triggerEvent: function () {
            return analogue.events.trigger.apply(window, arguments);
        }
    });
    analogue.extend(document, {
        addEvent: function (type, listener) {
            if ((/^(dom|ready)+(contentloaded)*$/i).test(type)) {
                if (!this.ready.fired) {
                    analogue.events.add.call(this, 'dataavailable', listener);
                } else {
                    listener.apply(this, arguments);
                }
                return this;
            } else {
                return analogue.events.add.call(this, type, listener);
            }
        },
        get: function (query) {
            var attempt = 'getElementById,getElementsByTagName,getElementsByClassName,getElementsBySelector'.split(','), i, ii, result;
            for (i = 0, ii = attempt.length; i < ii; i += 1) {
                try {
                    result = this[attempt[i]](query);
                    if (result !== null && (result.length === undefined || result.length !== 0)) {
                        return result;
                    }
                } catch (e) {}
            }
            return null;
        },
        head: document.getElementsByTagName('head')[0],
        loadScript: function (source) {
            if (/*@cc_on!@*/false && !this.ready.fired) {
                this.write(decodeURI('%3Cscript src="' + source + '" type="text/javascript"%3E%3C/script%3E'));
            } else {
                var script = this.createElement('script');
                script.setAttribute('type', 'text/javascript');
                script.setAttribute('src', source);
                this.head.appendChild(script);
            }
            return this;
        },
        loadStyleSheet: function (source, media) {
            var sheet = this.createElement('link');
            sheet.setAttribute('href', source);
            sheet.setAttribute('media', media || 'screen');
            sheet.setAttribute('rel', 'stylesheet');
            sheet.setAttribute('type', 'text/css');
            this.head.appendChild(sheet);
            return this;
        },
        ready: function () {
            if (!document.ready.fired) {
                document.ready.fired = true;
                document.ready.fire();
                if (document.removeEventListener) {
                    document.removeEventListener('DOMContentLoaded', document.ready, false);
                }
                window.removeEvent('load', document.ready);
            }
        }.extend({
            fire: (function (root) {
                if (root.dispatchEvent) {
                    return function () {
                        var event = document.createEvent('HTMLEvents');
                        event.initEvent('dataavailable', true, true);
                        document.dispatchEvent(event);
                    };
                } else if (root.fireEvent) {
                    return function () {
                        var event = document.createEventObject();
                        event.type = 'dataavailable';
                        document.fireEvent('on' + event.type, event);
                    };
                }
            }(document.documentElement)),
            fired: false
        }),
        set: function (tag, attributes, content) {
            var element, i, ii;
            if (attributes && !analogue.detect.setAttribute) {
                for (i in attributes) {
                    if (i.match(/^(checked|multiple|name|readonly|type)$/i)) {
                        tag += ' ' + i + '="' + attributes[i] + '"';
                        delete attributes[i];
                    }
                }
                tag = '<' + tag + '>';
            }
            element = this.createElement(tag);
            if (attributes) {
                for (i in attributes) {
                    if (attributes.hasOwnProperty(i)) {
                        element.setProperty(i, attributes[i]);
                    }
                }
            }
            if (content) {
                if (typeof content === 'string') {
                    element.appendChild(this.createTextNode(content));
                } else {
                    for (i = 0, ii = content.length; i < ii; i += 1) {
                        if (typeof content[i] === 'string') {
                            content[i] = this.createTextNode(content[i]);
                        }
                        element.appendChild(content[i]);
                    }
                }
            }
            return element;
        }
    });
    analogue.extend(document, analogue.methods);
    analogue.extend(analogue.elements, analogue.methods);
    if (!window.HTMLElement) {
        document.create = document.createElement;
        document.createElement = function (tag) {
            return analogue.extend(document.create(tag), analogue.elements);
        };
        document.addEvent('ready', function () {
            var element, elements = document.getElementsByTagName('*'), i;
            for (i = 0; (element = elements[i]); i += 1) {
                analogue.extend(elements[i], analogue.elements);
            }
        });
    } else {
        analogue.extend(window.HTMLElement.prototype, analogue.elements);
    }
    if (document.addEventListener) {
        document.addEventListener('DOMContentLoaded', document.ready, false);
        if (document.readyState) {
            (function () {
                if ((/loaded|complete/).test(document.readyState)) {
                    return document.ready();
                }
                setTimeout(arguments.callee, 10);
            }());
        }
    }
    /*@cc_on document.write(decodeURI('%3Cscript defer src="//:" onreadystatechange="if(this.readyState===\'complete\'){document.ready();}"%3E%3C/script%3E')); @*/
    window.addEvent('load', document.ready);
}(this));